import 'package:enterprise_v6_app/app/components/toast_widget.dart';
import 'package:enterprise_v6_app/app/components/update_dialog.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/utils/hover_button_util.dart';
import 'package:enterprise_v6_app/app/data/api/auth_apis.dart';
import 'package:enterprise_v6_app/app/modules/application/view.dart';
import 'package:enterprise_v6_app/app/modules/contact_us/view.dart';
import 'package:enterprise_v6_app/app/modules/profile/view.dart';
import 'package:enterprise_v6_app/app/modules/task/task_all/task_all_view.dart';
import 'package:enterprise_v6_app/global.dart';

class HomeController extends GetxController with GetSingleTickerProviderStateMixin {
  late TabController pageController;

  var index = 0.obs;

  final myTabs = <Widget>[
    ApplyPage(),
    TaskAllPage(),
    ContactUsPage(),
    MyPagePage(),
  ];

  final tabIcon = [
    AppImages.icon_home,
    AppImages.icon_task,
    AppImages.icon_contact,
    AppImages.icon_mine,
  ];

  final tabName = ['首页', '我的任务', '联系我们', '我的'];

  /// 上一次点击返回的时间
  DateTime lastBackPressTime = DateTime.now();

  /// WillPopScope退出判断
  Future<bool> exitReminder() async {
    if (DateTime.now().difference(lastBackPressTime) > const Duration(seconds: 2)) {
      AppToast.showWarning('再次点击退出');
      lastBackPressTime = DateTime.now();
    } else {
      return true;
    }
    return false;
  }

  /// 判断是否有升级
  void checkUpdate() async {
    final res = await AuthApis.appUpdate().execute();
    res.onSuccess((value) {
      if (value.data != null) {
        if (isUpdateVersion(value.data!.version, Global.packageInfo.version)) {
          // 有升级
          showDialog(
            barrierDismissible: false,
            context: Get.context!,
            builder: (context) {
            return WillPopScope(
              onWillPop: () async => false,
              child: UpdateDialog(
                downloadAddress: value.data!.downloadAddress ?? '',
                isForce: value.data!.isForcedUpgrade == 1,
                upDateContent: value.data!.upgradeDescription ?? '',
                version: value.data!.version ?? '',
              ),
            );
          },);
        }
      }
    });
  }

  /// 判断当前版本号和后台版本号是否一致
  /// [newVersion] 后台版本号
  /// [old] 当前版本号
  /// return true 需要升级 false 不需要升级
  bool isUpdateVersion(String? newVersion, String? old) {
    if (newVersion == null || newVersion.isEmpty || old == null || old.isEmpty) {
      return false;
    }
    int newVersionInt, oldVersion;
    final newList = newVersion.split('.');
    final oldList = old.split('.');
    if (newList.isEmpty || oldList.isEmpty) {
      return false;
    }
    for (int i = 0; i < newList.length; i++) {
      newVersionInt = int.parse(newList[i]);
      oldVersion = int.parse(oldList[i]);
      if (newVersionInt > oldVersion) {
        return true;
      } else if (newVersionInt < oldVersion) {
        return false;
      }
    }
    return false;
  }

  @override
  void onInit() {
    super.onInit();
    pageController = TabController(vsync: this, length: myTabs.length);
    HoverButtonUtil.open();
    // checkUpdate();
  }

  @override
  void onClose() {
    pageController.dispose();
    super.onClose();
  }

  // 关闭页面
  closePage() {
    /// 返回带参
    Get.back(result: {
      'one': 121212,
      'two': 2313131,
      'three': 13312432,
    });
  }

}
